<?php

namespace App\Http\Controllers\Backstage\Message;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;
//留言模块
class MessageController extends Controller
{
    //获取留言列表
    public function getMessage(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('message')->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['time'] = date('Y-m-d', $value['time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //搜索留言
    public function searchmessage(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $name = $request->input('name');
        $phone = (int)$request->input('phone');
        $email = $request->input('email');
        $message = $request->input('message');
        if ($name) {
            $map['name'] = $name;
        }
        if ($phone) {
            $map['phone'] = $phone;
        }
        if ($email) {
            $map['email'] = $email;
        }
        if ($message) {
            $map['message'] = $message;
        }
        $page = $request->input('page');
        $pageSize = $request->input('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        $info = DB::table('message')
            ->where('name', 'like', '%' . $name . '%')
            ->where('phone', 'like', '%' . $phone . '%')
            ->where('email', 'like', '%' . $email . '%')
            ->where('message', 'like', '%' . $message . '%')
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['time'] = date('Y-m-d', $value['time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
}
